﻿import React from 'react';
import {Link, hashHistory} from 'react-router';
import { Card, Form, Button, Input, message, Spin } from 'antd';
import classNames from 'classnames';
import {connect} from 'react-redux'
import SearchInput from '../../components/SearchInput'
import CompanyCard from './companyCard'
import OrderTimeLine from './orderTimeLine'
import ProfileCard from './profileCard'
import { orderQuery, timeLineQuery, orderCheck, checkOneUnmount } from '../../actions'

const FormItem = Form.Item;

class OrderCheckOne extends React.Component {

	constructor(props){
		super(props);
    this.state = {
      id:null,
      mode:'Edit',
      back:null,
    }
	}

  componentWillMount () {
    console.log('OrderCheckOne.componentWillMount')
    this.setState({mode:this.props.location.state.mode, back:this.props.location.state.back});
    this.props.dispatch(orderQuery.request()({id:this.props.location.state.studentId}));
    this.props.dispatch(timeLineQuery.request()({studentId:this.props.location.state.studentId}));
  }

  componentWillUnmount() {
    this.props.dispatch(checkOneUnmount()());
  }

  onBackClick(e) {
    e.preventDefault();
    hashHistory.goBack()
    //hashHistory.push('/order/check');
  }

  handleSubmit(type, id, e) {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((errors, values) => {
      if (!!errors) {
        console.log('Errors in form!!!');
        return;
      }
      console.log('Submit!!!');
      let order = { type:type, remark: values.remark, id:id };
      this.props.dispatch(orderCheck.request()(order));
    });
  }

	render() {
    let { company, timeLines, loading=false, result=null } = this.props;
    let profile = this.props.company.student == undefined ? {} : this.props.company.student;
    console.log(this.props);
    console.log("company: %o, profile: %o", company, profile);
    const formItemLayout = {
      labelCol: { span: 7 },
      wrapperCol: { span: 12 },
    };
    const { getFieldDecorator, getFieldError, isFieldValidating } = this.props.form;
    if (result != null) {
      message.success(result, 3);
    }

    let container = null;
    if (this.state.mode == 'Edit') {
      container = (<Card title="审批">
        <Spin spinning={loading}>
          <Form horizontal>
            <FormItem
              {...formItemLayout}
              label="remark">
              {getFieldDecorator('remark', {
                rules: [
                  { required: true, message: '请输入审批意见' },
                ],
              })(
                <Input type="textarea" placeholder="审批意见"  />
              )}
            </FormItem>

            <FormItem wrapperCol={{ span: 12, offset: 7 }}>
              <Button type="primary" disabled={(result != null)} onClick={this.handleSubmit.bind(this, 1, company.id)}>审批通过</Button>
              &nbsp;&nbsp;&nbsp;
              <Button type="primary" disabled={(result != null)} onClick={this.handleSubmit.bind(this, -1, company.id)}>审批不通过</Button>
            </FormItem>
          </Form>
        </Spin>
      </Card>);
    }
		return (
			<div>
        <Button style={{marginBottom:24}} type="primary" icon="arrow-left" onClick={this.onBackClick.bind(this)}>返回</Button>
				<Card style={{marginBottom:24}} title="学生信息"><ProfileCard profile={profile} loading={loading}/></Card>
				<Card style={{marginBottom:24}} title="用人单位信息"><CompanyCard company={company} loading={loading} /></Card>
				<Card style={{marginBottom:24}} title="签约审核记录"><OrderTimeLine timeLines={timeLines} loading={loading} /></Card>
        {container}
			</div>
		);
	}
}

function mapStateToProps(state) {
  console.log("state", state);
  return {
    loading:state.order.loading,
    company: state.order.company,
    timeLines: state.order.timeLines,
    result:state.order.result,
  };
}

OrderCheckOne = Form.create()(OrderCheckOne);

export default connect(mapStateToProps)(OrderCheckOne)
